﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NhaCungCap
{
    public partial class DanhSachSanPham : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string ma_loai = Request.QueryString["ma_loai"];
            if (ma_loai == null && ma_loai == "")
            {
                Session["msg"] = "Lỗi! Mã nhận vào không hợp lệ!";
                Session["msg_type"] = "error";
                Response.Redirect("TrangChu.aspx");
            }
           // SqlDataSource1.SelectCommand = "SELECT [TenTL], [TacGia], [MoTa], [HinhAnh], [DonGia], FROM [TaiLieu] WHERE IDLoaiTL=" + ma_loai;
            SqlDataSource1.SelectCommand = "SELECT *,(SELECT B2.TenLoaiTL FROM LoaiTaiLieu B2 WHERE B1.IDLoaiTL = B2.IDLoaiTL) TenLoaiTL FROM [TaiLieu] B1 WHERE IDLoaiTL=" +ma_loai+ " ORDER by B1.ThuTuHienThi";
           if (!IsPostBack)
                ListView1.DataBind();

        }

        protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (String.Equals(e.CommandName, "AddToCard"))
            {
                ListViewDataItem dataItem = (ListViewDataItem)e.Item;
                TextBox tb = (TextBox)e.Item.FindControl("TextBox1");
                string sl = tb.Text;

                int soluong = System.Int32.Parse(sl);

                string IDTaiLieu =
                  ListView1.DataKeys[dataItem.DisplayIndex].Value.ToString();
                string ten = ListView1.DataKeys[dataItem.DisplayIndex].Values["TenTL"].ToString();
                string gia = ListView1.DataKeys[dataItem.DisplayIndex].Values["DonGia"].ToString();
                int dongia = System.Int32.Parse(gia.Replace(".", ""));
                int ID = System.Int32.Parse(IDTaiLieu);
                long thanh_tien = dongia * soluong;
                //ShoppingCart tl = new ShoppingCart(ID,ten,dongia,soluong,thanh_tien);

                //Session.Add("gio_hang",tl);

                ShoppingCart card = (ShoppingCart)Session["gio_hang"];
                if (card == null)
                    card = new ShoppingCart();
                card.Add(ID, ten, dongia, soluong, thanh_tien);
                Session["gio_hang"] = card;

                ASP.masterpage_master master = (ASP.masterpage_master)this.Master;
                master.RefreshShoppingCartInfo();
                /*
                if (SelectedEmployeesListBox.Items.FindByValue(IDTaiLieu) == null)
                {
                    ListItem item = new ListItem(e.CommandArgument.ToString(), IDTaiLieu);
                    SelectedEmployeesListBox.Items.Add(item);
                }
                 */
            }
        }

        public string format_tt(string tt)
        {

            string rs = tt;
            //thay dau ',' thanh '.'
            int l = (rs.Length - 1) / 3;
            for (int i = 0; i < l; i++)
            {
                rs = rs.Insert(rs.Length - (i + 1) * 3 - i, ".");
            }

            return rs;
        }
    }
}